//
//  HQDetailViewController.m
//  HaoZhouDao
//
//  Created by quan on 15/11/16.
//  Copyright © 2015年 quan. All rights reserved.
//

#import "HQDetailViewController.h"
#import "HQZhou.h"
#import "Masonry.h"
#import "UIView+Extension.h"
#import "HQViewController.h"
#import "SVProgressHUD.h"
#import "UIButton+Extension.h"


@interface HQDetailViewController () <UIScrollViewDelegate>
//拖动
@property (nonatomic, strong) UIScrollView *scrollView;
//scrollView里面的view
@property (nonatomic, strong) UIView *bigView;
//在图片的大view
@property (nonatomic, strong) UIView *iconView;
//材料View
@property (nonatomic, strong) UIView *ingredientsView;
//做法view
@property (nonatomic, strong) UIView *stepsView;



//详情的iconView内部
@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *tasteLabel;

//详情材料内部
@property (nonatomic, strong) UILabel *caiLiao;
@property (nonatomic, strong) UILabel *ingredients;

//详情做法内部
@property (nonatomic, strong) UILabel *zuoFa;
@property (nonatomic, strong) UILabel *steps;

@end

@implementation HQDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"详细信息";

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
    
    //添加左边按钮
    UIButton *leftButton = [UIButton buttonWithImage:@"nav_button" title:@"关闭" font:15 setColorRed:0.798 green:0.613 blue:0.392];
    [leftButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    
    //根据转过来的控制器,来确定右导航按钮的属性
    if ([self.fromVC isEqualToString:@"searchVC"] || [self.fromVC isEqualToString:@"mainVC"]) {
        
        UIButton *rightButton = [UIButton buttonWithImage:@"nav_share_button" title:nil font:15 setColorRed:0.798 green:0.613 blue:0.392];
        [rightButton addTarget:self action:@selector(putIn:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        
    }
    else if([self.fromVC isEqualToString:@"favoriteVC"]) {
        
        UIButton *rightButton = [UIButton buttonWithImage:@"nav_button" title:@"购买" font:15 setColorRed:0.798 green:0.613 blue:0.392];
        [rightButton addTarget:self action:@selector(buy) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    }

    
    //加载子控件
    [self setupView];
}

- (void)buy {
    //收藏菜品的动作
    UIAlertAction *alertOne = [UIAlertAction actionWithTitle:@"直接购买" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"确定购买");
        }];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"点击了取消");
        }];
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"购买" message:@"您将以18块的购买清单所列粥品" preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:cancelAction];
        [alertController addAction:sureAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
        
    }];
    
    //放入购物车的动作
    UIAlertAction *alertTwo = [UIAlertAction actionWithTitle:@"放入购物车" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        
        NSString *filePath = [docPath stringByAppendingPathComponent:@"buy.archieve"];
        //        NSLog(@"%@",filePath);
        
        NSMutableArray *zhousM = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        if (zhousM != nil) {
            //                NSLog(@"之前有内容");
            
            BOOL exist = NO;
            
            for (HQZhou *zhou in zhousM) {
                if ([zhou.name isEqualToString:self.zhou.name]) {
                    exist = YES;
                    break;
                }
            }
            
            if (exist == NO) {
                [zhousM addObject:self.zhou];
                [NSKeyedArchiver archiveRootObject:zhousM toFile:filePath];
            }
            
            
            
        }
        else {
            //                NSLog(@"之前没有内容");
            NSMutableArray *zhousM = [NSMutableArray array];
            [zhousM addObject:self.zhou];
            [NSKeyedArchiver archiveRootObject:zhousM toFile:filePath];
            //                NSLog(@"%@",zhousM);
        }


            }];
    
    UIAlertAction *alertCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    [alertController addAction:alertOne];
    [alertController addAction:alertTwo];
    [alertController addAction:alertCancel];
    
    [self presentViewController:alertController animated:YES completion:nil];
}


- (void)putIn:(id)sender
{
    //收藏菜品的动作
    UIAlertAction *alertOne = [UIAlertAction actionWithTitle:@"收藏菜品" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        NSLog(@"点击了收藏");

        
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

        NSString *filePath = [docPath stringByAppendingPathComponent:@"favorite.archieve"];
//        NSLog(@"%@",filePath);
//        if (filePath) {
            NSMutableArray *zhousM = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
            if (zhousM != nil) {
//                NSLog(@"%@",zhousM);
                
                BOOL exist = NO;
                
                for (HQZhou *zhou in zhousM) {
                    if ([zhou.name isEqualToString:self.zhou.name]) {
                        exist = YES;
                        break;
                    }
                }
                
                if (exist == NO) {
                    [zhousM addObject:self.zhou];
                    [NSKeyedArchiver archiveRootObject:zhousM toFile:filePath];
                }
   
            }
            else {
//                NSLog(@"之前没有内容");
                NSMutableArray *zhousM = [NSMutableArray array];
                [zhousM addObject:self.zhou];
                [NSKeyedArchiver archiveRootObject:zhousM toFile:filePath];
//                NSLog(@"%@",zhousM);
            }

         [SVProgressHUD showSuccessWithStatus:@"成功放入收藏"];
   
    }];
    
    //放入购物车的动作
    UIAlertAction *alertTwo = [UIAlertAction actionWithTitle:@"放入购物车" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//        NSLog(@"点击了购物车");
        
        
        
        
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        
        NSString *filePath = [docPath stringByAppendingPathComponent:@"buy.archieve"];
//        NSLog(@"%@",filePath);

            NSMutableArray *zhousM = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
            if (zhousM != nil) {
                //                NSLog(@"之前有内容");
                
                BOOL exist = NO;
                
                for (HQZhou *zhou in zhousM) {
                    if ([zhou.name isEqualToString:self.zhou.name]) {
                        exist = YES;
                        break;
                    }
                }
                
                if (exist == NO) {
                    [zhousM addObject:self.zhou];
                    [NSKeyedArchiver archiveRootObject:zhousM toFile:filePath];
                }

                
                
            }
            else {
                //                NSLog(@"之前没有内容");
                NSMutableArray *zhousM = [NSMutableArray array];
                [zhousM addObject:self.zhou];
                [NSKeyedArchiver archiveRootObject:zhousM toFile:filePath];
                //                NSLog(@"%@",zhousM);
            }
        
        [SVProgressHUD showSuccessWithStatus:@"成功放入购物车"];
     
    }];
    
    UIAlertAction *alertCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    [alertController addAction:alertOne];
    [alertController addAction:alertTwo];
    [alertController addAction:alertCancel];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}



- (void)setupView{
    
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.bigView];

    [self.bigView addSubview:self.iconView];
    [self.bigView addSubview:self.ingredientsView];
    [self.bigView addSubview:self.stepsView];


    //约束iconView
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(self.bigView).with.offset(10);
        make.left.equalTo(self.bigView).with.offset(10);
        make.right.equalTo(self.bigView).with.offset(-10);
        make.height.mas_equalTo(@370);

    }];
    
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.iconView).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.bgView).insets(UIEdgeInsetsMake(20, 20, 40, 20));
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).with.offset(15);
        make.bottom.equalTo(self.bgView).with.offset(-15);
        make.height.mas_equalTo(@20);
    }];
    
    [self.tasteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView).with.offset(-55);
        make.bottom.equalTo(self.bgView).with.offset(-15);
    }];
    
    /**
     *   //约束材料view
     *
     */
    [self.ingredientsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.mas_bottom).with.offset(20);
        make.left.equalTo(self.bigView).with.offset(10);
        make.right.equalTo(self.bigView).with.offset(-10);
//        make.height.mas_equalTo(@100);
    }];
    
    [self.caiLiao mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ingredientsView).with.offset(10);
        make.left.equalTo(self.ingredientsView).with.offset(10);
        make.height.mas_equalTo(@20);
        
    }];
    
    [self.ingredients mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.caiLiao.mas_bottom).with.offset(5);
        make.right.equalTo(self.ingredientsView).with.offset(-10);
        make.left.equalTo(self.ingredientsView).with.offset(10);
        
    }];
    
    [self.ingredientsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.ingredients.mas_bottom);
    }];
    
    /**
     *  约束做法view
     *
     */
    
    [self.stepsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ingredientsView.mas_bottom).with.offset(10);
        make.left.equalTo(self.bigView).with.offset(10);
        make.right.equalTo(self.bigView).with.offset(-10);
    }];
    
    [self.zuoFa mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.stepsView).with.offset(10);
        make.left.equalTo(self.stepsView).with.offset(10);
        make.height.mas_equalTo(@20);
    }];
    
   [self.steps mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.zuoFa.mas_bottom).with.offset(5);
        make.right.equalTo(self.stepsView).with.offset(-10);
        make.left.equalTo(self.stepsView).with.offset(10);
       
    }];
    
    [self.stepsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.steps.mas_bottom);
    }];



}


//退出controller方法
- (void)dismiss{
    [self dismissViewControllerAnimated:true completion:nil];
}


//懒加载

- (UIScrollView *)scrollView{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.delegate = self;
        _scrollView.scrollEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake(0, 800);
        _scrollView.height -= 64;
        _scrollView.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);
    }
    return _scrollView;
}

- (UIView *)bigView{
    if (_bigView == nil) {
        _bigView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
//        _bigView = [[UIView alloc] init];
    }
    return _bigView;
}

/**
 *  上面那个有图片的View
 */
- (UIView *)iconView{
    
    if (_iconView == nil) {
        
        
        //创建子控件
        _iconView = [[UIView alloc] init];
        self.bgView = [[UIImageView alloc] init];
        self.imageView = [[UIImageView alloc] init];
        self.nameLabel = [[UILabel alloc] init];
        self.tasteLabel = [[UILabel alloc] init];
        
        //赋值背景
        self.bgView.backgroundColor = [UIColor colorWithRed:1.000 green:0.977 blue:0.876 alpha:1.000];
        self.bgView.layer.cornerRadius = 8;
        self.bgView.clipsToBounds = YES;
        self.bgView.image = [UIImage imageNamed:@"frame_photo_big"];
        
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:_zhou.photo ofType:nil];
        self.imageView.image = [UIImage imageWithContentsOfFile:imagePath];
        [self.imageView sizeToFit];
        
        self.nameLabel.text = _zhou.name;
        self.nameLabel.textColor = [UIColor orangeColor];
        self.nameLabel.font = [UIFont systemFontOfSize:16];
        
        self.tasteLabel.textColor = [UIColor lightGrayColor];
        self.tasteLabel.text = [NSString stringWithFormat:@"口味:%@",_zhou.flavor];
        
        //添加到你控件
        [_iconView addSubview:self.bgView];
        [_iconView addSubview:self.imageView];
        [_iconView addSubview:self.nameLabel];
        [_iconView addSubview:self.tasteLabel];

    }

    return _iconView;
}

//材料View
- (UIView *)ingredientsView{
    if (_ingredientsView == nil) {
        
        //创建子view
        _ingredientsView = [[UIView alloc] init];
        self.caiLiao = [[UILabel alloc] init];
        self.ingredients = [[UILabel alloc] init];
        
        //赋值,设定
        self.caiLiao.text = @"【材料】";
        self.ingredients.textColor = [UIColor lightGrayColor];
        self.ingredients.font = [UIFont systemFontOfSize:16];
        self.ingredients.numberOfLines = 0;
        self.ingredients.text = _zhou.ingredients;
        
        [_ingredientsView addSubview:self.caiLiao];
        [_ingredientsView addSubview:self.ingredients];

        
    }
    return _ingredientsView;
}

- (UIView *)stepsView{
    if (_stepsView == nil) {
        _stepsView = [[UIView alloc] init];
        self.zuoFa = [[UILabel alloc] init];
        self.steps = [[UILabel alloc] init];
        
        
        //赋值,设定
        self.zuoFa.text = @"【做法】";
        self.steps.textColor = [UIColor lightGrayColor];
        self.steps.font = [UIFont systemFontOfSize:16];
        self.steps.numberOfLines = 0;
        self.steps.text = _zhou.steps;
        
        
        
        [_stepsView addSubview:self.zuoFa];
        [_stepsView addSubview:self.steps];

    }
    return _stepsView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
